<?php
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
	$('.search-form').toggle();
	return false;
});
$('.search-form form').submit(function(){
	$.fn.yiiGridView.update('project-grid', {
		data: $(this).serialize()
	});
	return false;
});
");
?>

<h1><?php echo Yii::t('project','Projects Overview');?></h1>
<?php echo CHtml::link(Yii::t('project','Create Project'), array('project/create'), array('class'=>'btn btn-primary')); ?>
<?php $this->widget('bootstrap.widgets.TbGridView',array(
	'id'=>'project-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		array(
			'name' => 'name',
			'header'=>Yii::t('project','Project Name'),
		),
		array(
			'name' => 'county_id',
			'header'=>Yii::t('project','Belonging County'),
			'value'=>'$data->county->name',
		),
		array(
			'name' => 'description',
			'header'=>Yii::t('project','Investment'),
			'type' => 'raw',
			'value'=>function($data){
						$investProgress = '0%';
						$progressBarClass = 'progress-success';
						$investProgressStr = '0%';
						
						$isProjectStarted = false;
						
						$totalRealInvestUsed = 0;
						$totalPlanInvest = 0;
						foreach($data->unitProjects as $eachUnitProject) {
							foreach($eachUnitProject->segmentProjects as $eachSegmentProject) {
								if($eachSegmentProject->start_work_status == 1) $isProjectStarted = true;
								
								$totalRealInvestUsed += $eachSegmentProject->real_invest_used;
								$totalPlanInvest += $eachSegmentProject->plan_invest;
							}
						}

						if($isProjectStarted) {
							if($totalRealInvestUsed != 0 && $totalPlanInvest != 0) {
								$investProgress = strval(round(($totalRealInvestUsed/$totalPlanInvest), 2) * 100) . '%';
								$investProgressStr = $investProgress;
									
								if(($totalRealInvestUsed/$totalPlanInvest) > 1) {
									$investProgress = '100%';	//Over 100% is meaningless
									$progressBarClass = 'progress-danger';
								}
							}
							
							return '<div class="progress ' . $progressBarClass . ' progress-striped" style="margin-bottom: 9px;">
									<div class="bar" style="width:' . $investProgress . '">' . $investProgressStr . '</div>
								</div>';
						} else {
							return '<p class="muted">' . Yii::t('project','Not Started') . '</p>';;
						}
					},
		),
		array(
			'name' => 'description',
			'header'=>Yii::t('project','Schedule'),
			'type' => 'raw',
			'value'=>function($data){
							$projectStarted = 0;
							$projectFinished = 0;
							$isProjectStarted = false;
							foreach($data->unitProjects as $eachUnitProject) {
								foreach($eachUnitProject->segmentProjects as $eachSegmentProject) {
									if($eachSegmentProject->start_work_status == 1) {
										$isProjectStarted = true;
										$projectStarted++;
									}
								
									if($eachSegmentProject->end_work_status == 1) {
										$projectFinished++;
									}
								}
							}
							
							if($isProjectStarted) {
								$scheduleRate = round(($projectFinished/($projectStarted)), 2) * 100;
								$scheduleProgressStr = strval($scheduleRate) . '%';
								if($scheduleRate < 10) {
									$progressBarClass = 'progress-danger';
								} else if($scheduleRate < 40) {
									$progressBarClass = 'progress-warning';
								} else if($scheduleRate < 70) {
									$progressBarClass = 'progress-info';
								} else {
									$progressBarClass = 'progress-success';
								}
								$scheduleContent = '<div class="progress ' . $progressBarClass . ' progress-striped" style="margin-bottom: 9px;">
									<div class="bar" style="width:' . $scheduleProgressStr . '">' . $scheduleProgressStr . '</div>
								</div>';
							} else {
								$scheduleContent = '<p class="muted">' . Yii::t('project','Not Started') . '</p>';
							}
							return $scheduleContent;
					},
		),
		array(
				'class'=>'bootstrap.widgets.TbButtonColumn',
				'template'=>'{view} {update}',
				'htmlOptions'=>array('width'=>'80px'),
				//'template'=>'{view}{update}{delete}',
				'buttons'=>array
				(
					'view' => array('url'=>'$this->grid->controller->createUrl("/live/view", array("cid"=>$data["county"]["id"], "pid"=>$data["id"]))'),
					'update' => array('url'=>'$this->grid->controller->createUrl("/project/status", array("id"=>$data["id"]))'),
				),
		),
	),
)); ?>
